package com.kuang.test;

import org.junit.Test;

import java.security.PublicKey;
import java.sql.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName LeeCode
 * @Description: TODO
 * @Author: Sunny
 * @Create: 2024-08-25 18:08
 * @Version 1.0
 **/
public class LeeCode {
    /**
     * 冒泡排序
     */
    @Test
    public void leeCode1(){
     int[] array = {5,6,6,4,3};
     System.out.println(Arrays.toString(array));
     System.out.println(Arrays.toString(bubbleSort(array)));

    }

    public int[] bubbleSort(int[] array){
        int temp ;
        for (int i = 0; i < array.length -1; i++) {
            for (int j = 0; j < array.length-i-1; j++) {
                if (array[j]>array[j+1]) {
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }

            }
        }
        return array;
    }

    /**
     * 两数之和
     */
    @Test
    public void leeCode2(){
        int[]nums = {1,2,3,4,5,6};
        int target = 9;
        System.out.println(Arrays.toString(youyaTwoSum(nums,target)));
    }

    public int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (i!=j && nums[i]+nums[j] == target) {
                    return new int[] {i,j};
                }
            }
        }
        return null;
    }

    public int[] youyaTwoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>(nums.length);
        for (int i = 0; i < nums.length; i++) {
            int i1 = target - nums[i];
            if(map.containsKey(i1)){
               return new int[] {map.get(i1),i};
           }
            map.put(nums[i],i);
        }
        return null;
    }

    /**
     * 整数反转
     */
    @Test
    public void leeCode3(){
        int x = -1123;
        System.out.println(reverse(x));
    }

    public int reverse(int x){
        //1.判断是否为负数
        String xstr = x+"";
        String fs = "";
        if(xstr.charAt(0) == '-'){
            fs = "-";
            xstr = xstr.substring(1);
        }
        //2.进行反转
        String res = "";
        for(int i = xstr.length()-1;i>=0;i--){
            res += xstr.charAt(i);
        }
        //3.返回反转后的结果
        return Integer.parseInt(fs + res);



    }
    @Test
    public void leeCode4(){
            int []aa = {0,1,2,3,4,1};
        System.out.println(findReplaceNumb(aa));
    }
    //找到数组重复的元素
    public int findReplaceNumb(int[] numbs) {
        for (int i = 0; i < numbs.length; i++) {
            while (i != numbs[i]) {
                //判断元素是否在正确的位置上，是就返回，否者就交换
                if (numbs[i] == numbs[numbs[i]]) {
                    return numbs[i];
                }
                int k = numbs[numbs[i]];
                numbs[numbs[i]] = numbs[i];
                numbs[i] =k ;

            }
        }
        return -1;
    }
    //在二维数组中找到目标数据
    @Test
    public void leeCode5(){
        int [] [] array = {{1,2,3},{4,5,6},{7,8,9},{10,11,12},{23,24}};
        boolean numberIn2DArray = findNumberIn2DArray(array, 12);
        System.out.println(numberIn2DArray);

    }
    public boolean findNumberIn2DArray(int [][] matrix,int target){
        if(matrix == null || matrix.length<= 0 || matrix[0].length <= 0){
            return false;
        }
        int cols = matrix.length;
        int rows = matrix[0].length;
        int col = cols-1;
        int row = 0;
        while (col >= 0 && row < rows) {
            if (target > matrix[col][row]) {
                row ++;
            } else if (target < matrix[col] [row]){
                col --;
            }else {
                return true;
            }
        }
        return false;
    }
    //替换空格
    @Test
    public void leeCode6(){
        String aa = "erwegwegweg aff asdsa a  fff";
        System.out.println(replaceSpace(aa));

    }
    //常规
    public String replaceSpace(String s){
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) ==' '){
                stringBuffer.append("%20");
            }else {
                stringBuffer.append(s.charAt(i));
            }
        }
        return stringBuffer.toString();
    }
    @Test
    public void leeCode7(){
        char[] a = {'a','b','c','d','e','f'};
        System.out.println( reverseString(a));
    }
    //反转字符串
    public char[] reverseString(char[] s) {
        int left = 0;
        int right = s.length-1;
        while (left<right){
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left ++;
            right --;
        }
        return s;
    }
}
